import type {EventHandler, EventHandlerRequest} from 'h3'
import {consola} from "consola";
import {I18N_PREFIX, RESULT_CODE_ERROR_COMMON} from "~~/server/resources/constants/constant";
import {ResponseResult} from "~~/server/resources/types/response-result";

export const defineWrappedHandler = <T extends EventHandlerRequest, D>(
    handler: EventHandler<T, D>
): EventHandler<T, D> =>
    defineEventHandler<T>(async event => {
        try {
            const response = await handler(event)
            consola.info("请求接口正常结果", response)
            return response;
        } catch (err) {
            consola.error("请求接口异常结果", err)
            const t = await useTranslation(event);
            return new ResponseResult(RESULT_CODE_ERROR_COMMON, err, t(I18N_PREFIX + RESULT_CODE_ERROR_COMMON));
        }
    })